<?php


class Sample {
    const API_KEY = "";
    const SECRET_KEY = "";

    
        
    public function run() {
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file_upload'])) {

            //$upload_dir = '/'; // 确保这个目录已经存在并且可写
            $upload_file = basename($_FILES['file_upload']['name']);
         
            if (move_uploaded_file($_FILES['file_upload']['tmp_name'], $upload_file)) {
                echo "文件上传成功！";
            } else {
                echo "文件上传失败！";
            }
        
        $image = base64_encode(file_get_contents($upload_file));
        //$image1 = file_put_contents('1.png',base64_decode($image));
        //echo $image1;
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://aip.baidubce.com/rest/2.0/image-process/v1/image_quality_enhance?access_token={$this->getAccessToken()}",
            CURLOPT_TIMEOUT => 30,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_CUSTOMREQUEST => 'POST',
            // image 可以通过 $this.getFileBase64Content("C:\fakepath\R.jpg") 方法获取
            CURLOPT_POSTFIELDS => http_build_query(array(
    'image' => $image
    )),
    
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/x-www-form-urlencoded',
                'Accept: application/json'
            ),

        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
        }else {
            ?>
            <html>
                <head>
                    <title>
                    大星（无损放大）
                    </title>
                </head>
                <body>
                    <center><h1>大星（无损放大）</h1></center>
                    <form action="big.php" method="post" enctype="multipart/form-data">
                    <center>选择文件：<input type="file" name="file_upload">
                        <input type="submit" value="上传"></center>
                    </form>
                </body>
            </html>
            <?php
            }
    }
    
     /**
     * 获取文件base64编码
     * @param string  $path 文件路径
     * @return string base64编码信息，不带文件头
     */
    private function getFileContentAsBase64($path){
        
        return base64_encode(file_get_contents($path));
      
    }
    
    /**
     * 使用 AK，SK 生成鉴权签名（Access Token）
     * @return string 鉴权签名信息（Access Token）
     */
    private function getAccessToken(){
        $curl = curl_init();
        $postData = array(
            'grant_type' => 'client_credentials',
            'client_id' => self::API_KEY,
            'client_secret' => self::SECRET_KEY
        );
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://aip.baidubce.com/oauth/2.0/token',
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => http_build_query($postData)
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        $rtn = json_decode($response);
        return $rtn->access_token;
    }
}

$rtn = (new Sample())->run();
//print_r($rtn);
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['file_upload'])) {
$target = json_decode($rtn);
$target1 = $target->image;
$target2 = base64_decode($target1);
// 使用 mt_rand() 生成一个随机的10位数
$randomNumber = mt_rand(1000000000, 9999999999);
//echo $randomNumber;
$filename = $randomNumber . '.jpg';
$result = file_put_contents($filename,$target2);

    echo '<script>window.location.href = "'.$filename.'";</script>';
}



    
